package com.wx.fresh.custom.controller;

import com.wx.fresh.common.util.CommonResult;
import com.wx.fresh.custom.vo.userVo.CustomLoginOutVO;
import com.wx.fresh.custom.vo.userVo.CustomLoginVO;
import com.wx.fresh.custom.vo.userVo.CustomRegisterVO;
import com.wx.fresh.user.api.LoginFeignClient;
import com.wx.fresh.user.dto.CustomLoginDto;
import com.wx.fresh.user.dto.CustomLoginOutDto;
import com.wx.fresh.user.dto.CustomRegisterDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * custom权限校验模块controller
 *
 * @author salthe
 * @date 2022/4/21
 */


@RestController
@RequestMapping("/custom")
@Api(tags = "custom权限校验")
public class LoginController {

    @Autowired
    private LoginFeignClient loginFeignClient;

    @PostMapping("/register")
    @ApiOperation(value = "custom用户注册")
    public CommonResult<Integer> registerUser(@RequestBody CustomRegisterVO customRegisterVO) {

        CustomRegisterDto customRegisterDto = new CustomRegisterDto();
        //将Vo转成Dto
        BeanUtils.copyProperties(customRegisterVO,customRegisterDto);

        return CommonResult.getModel(loginFeignClient.registerUser(customRegisterDto));
    }

    @PostMapping("/login")
    @ApiOperation(value = "custom用户登录")
    public CommonResult<CustomLoginOutVO> loginUser(@RequestBody CustomLoginVO customLoginVO) {

        CustomLoginOutVO customLoginOutVO = new CustomLoginOutVO();
        CustomLoginDto customLoginDto = new CustomLoginDto();
        BeanUtils.copyProperties(customLoginVO,customLoginDto);

        CommonResult<CustomLoginOutDto> login = loginFeignClient.customLogin(customLoginDto);
        BeanUtils.copyProperties(login.getData(),customLoginOutVO);
        return CommonResult.success(customLoginOutVO);
    }

}
